<?php
namespace app\index\validate;
use think\Request;
use think\Validate;
use think\Exception ;

/**
 * Class BaseValidate
 * 验证类的基类
 */
class BaseValidate extends Validate
{
    /**
     * 检测所有客户端发来的参数是否符合验证类规则
     * 基类定义了很多自定义验证方法
     * 这些自定义验证方法其实，也可以直接调用
     * @throws ParameterException
     * @return true
     */
    public function goCheck()
    {
        //必须设置contetn-type:application/json
        $request = Request::instance();
        $params = $request->param();
        $result = $this->check($params) ;
        if(!$result){
            $error =$this->error ;
            throw new Exception($error);
            // return [0,$error] ;
        }else{
            return 1 ;
        }
    }    

    public function goapiCheck()
    {
        //必须设置contetn-type:application/json
        $request = Request::instance();
        $params = $request->param();
        $result = $this->check($params) ;
        if(!$result){
            $error =$this->error ;
            return [0, $error];
        }else{
            return [1 , ''];
        }
    }



    protected function isPositiveInteger($value, $rule='', $data='', $field='')
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        }
        return $field . '必须是正整数';
    }

    protected function isNotEmpty($value, $rule='', $data='', $field='')
    {
        if (empty($value)) {
            return $field . '不允许为空';
        } else {
            return true;
        }
    }


}